import { Injectable } from '@angular/core';
import { ApiService } from '@core/services/api';
import { _HttpClient } from '@delon/theme';
import { Observable, Subject } from 'rxjs';
export interface TreeNodeItem {
  key: string,
  title: string,
  children?: TreeNodeItem[],
  [key:string]: any
}
@Injectable({
  providedIn: 'root'
})
export class AreaService {
  location = new Subject();
  getLocation = () => {
    return this.location.asObservable();
  }
  getAreaTreeNode(id): Promise<any> {
    return new Promise<any>((resolve, reject) => {
      this.http.post(this.api.city.tree.url, {id}).subscribe(res => {
        if (res.data && res.data.length > 0) {
          const node = res.data.map(item => {
            return {
              key: item.id,
              title: item.name,
              level: item.cityRank,
              rank: Number(item.rank),
              parentAllId: item.parentAllId,
              parentId:item.parentId
            }
          })
          resolve(node);

        } else {
          resolve([])
        }
      })
    })
  }

  getAreaByPId(id): Observable<any> {
    return this.http.request<any>(this.api.city.tree.method, this.api.city.tree.url, {body: {id}})
  }
  getCityList(): Observable<any> {
    return this.http.request<any>(this.api.city.list.method,this.api.city.list.url, {body: {}});
  }
  constructor(
    private api: ApiService,
    private http: _HttpClient
  ) { }
}
